package com.songcha.sakura.server;


import java.io.File;
import java.io.FileInputStream;

import fi.iki.elonen.NanoHTTPD;

public class MyServer extends NanoHTTPD {
    public static String M3U8="application/x-mpegURL";
    public static String Video="video/mp4";
    public static String text="video/mp4";
    public static String TS="video/mp2t";
    //public String mVideoPath="";

    public MyServer(int port) {
        super(port);
        //this.mVideoPath=videoPath;
    }

  /*  public MyServer(int port, Context context) {
        super(port);
    }*/

    public MyServer(String hostname, int port) {
        super(hostname, port);
    }

    @Override
    public Response serve(IHTTPSession session) {

        try {
            //文件下载得保存路径
            //String path = videoPath;
            //本地地址转为网络地址

           /* if (session.getUri().contains(MyApplication.Companion.getContext().getFilesDir().getAbsolutePath())) {
                File f = new File(session.getUri());
                FileInputStream inputStream = new FileInputStream(f);
                return newFixedLengthResponse(Response.Status.OK, M3U8, inputStream,f.length());
            }*/

            File file = new File(session.getUri());
            //判断本地的文件是否存在
            if (file.exists()) {
                //直接返回m3u8的文件格式
                return newChunkedResponse(Response.Status.OK, "video/x-mpegURL", new FileInputStream(file));
            } else {
                return newFixedLengthResponse(Response.Status.NOT_FOUND, "text/html", "文件不存在");
            }


            //默认页面
           // String msg = "<html><body><h1>Hello server</h1></body></html>";
           // return newFixedLengthResponse(msg);
        }catch (Exception e){
            e.printStackTrace();
            String msg = "<html><body><h1>服务器出错</h1></body></html>";
            return newFixedLengthResponse(msg);
        }
    }
}
